package com.itheima.day10;

import com.itheima.domain.Student;
import com.itheima.util.RandomUtil;

import javax.print.DocFlavor;
import java.time.LocalDate;
import java.time.Period;
import java.util.*;

/*
各个小组的平均年龄？
最大三个年纪的
最小三个年纪的
最大年纪的人和最小年纪的人是谁，差多久?
不同的姓各自有多少学生？
不同的年龄段有多少人？
 */
public class Test01 {

    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        for (int i = 1; i <= 45; i++) {
            Student student = new Student();
            String sid = "";
            if (i <= 9) {
                sid += "A000" + i;
            } else {
                sid = "A00" + i;
            }
            student.setSid(sid);
            student.setName(RandomUtil.getXing() + RandomUtil.getMing());
            student.setGender(RandomUtil.getGender());
            student.setBirthday(RandomUtil.getBirthday());
            student.setPhone(RandomUtil.getPhone());
            student.setWxs(RandomUtil.getWxs());
            student.setGroup((i - 1) / 5 + 1);//i%10+1
            list.add(student);
        }
        /*for (Student s : list) {
            System.out.println(s);
        }*/
        //tongjiAvgAge(list);
        tongjiBigest3(list);
        tongjiYoungest3(list);
        getOldestAndYoungest(list);
        tongjiXing(list);
        tongjiNianlingDuan(list);
    }

    //各个小组的平均年龄？
    public static void tongjiAvgAge( ArrayList<Student> list){
        //Map  键 组名   值 :ArrayList<Student>
        TreeMap<Integer, ArrayList<Student>> map = new TreeMap<>();
        for (Student student : list) {
            int group = student.getGroup();
            //判断map中是否有这个组
            boolean flag = map.containsKey(group);
            if(flag){
                ArrayList<Student> list1 = map.get(group);
                //有 将学生添加到集合中
                list1.add(student);
                //用行的集合 替换 老的集合
            }else {  //没有  创建一个新的集合 存入map中
                ArrayList<Student> list1 = new ArrayList<>(List.of(student));
                map.put(group,list1);
            }
        }
        map.forEach((k,v)-> System.out.println(k+"组的人有"+v));
        //Map 键 组名 值 平均年龄
        Map<Integer, Double> ageMap = new TreeMap<>();
        map.forEach((k,v)-> {
            ArrayList<Student> list1 = v;
            int count=0;
            for (Student student : list1) {
                count+=student.getAge();
            }
            double avgAge=count*1.0/ list1.size();
            ageMap.put(k,avgAge);
        });
        ageMap.forEach((k,v)-> System.out.println(k+"组的平均年龄是"+v));



    }


    //最大三个年纪的

    public static void tongjiBigest3( ArrayList<Student> list){
        TreeSet<Student> treeSet = anxufangruSet(list);
        treeSet.stream().limit(3).forEach(s-> System.out.println(s));
        // System.out.println(set);


    }

    public static TreeSet<Student> anxufangruSet(ArrayList<Student> list) {
        TreeSet<Student> set = new TreeSet<>((s1, s2) -> {
            LocalDate birthday1 = s1.getBirthday();
            LocalDate birthday2 = s2.getBirthday();
            int result = birthday1.getYear() - birthday2.getYear();
            result = result == 0 ? birthday1.getMonthValue() - birthday2.getMonthValue() : result;
            result = result == 0 ? birthday1.getDayOfMonth() - birthday2.getDayOfMonth() : result;
            result = result == 0 ? s1.getName().compareTo(s2.getName()) : result;
            return result;
        });
        set.addAll(list);
        return set;
    }
    //最小三个年纪的
    public static void tongjiYoungest3( ArrayList<Student> list){
        TreeSet<Student> treeSet = anxufangruSet(list);
        treeSet.stream().skip(list.size()-3).forEach(s-> System.out.println(s));
        // System.out.println(set);


    }
    //最大年纪的人和最小年纪的人是谁，差多久?
    public static void getOldestAndYoungest(ArrayList<Student> list){
        TreeSet<Student> treeSet = anxufangruSet(list);
        Student first = treeSet.first();
        System.out.println("最老的:"+first);
        Student last = treeSet.last();
        System.out.println("最小的:"+last);
        Period between = Period.between(first.getBirthday(), last.getBirthday());
        System.out.println("俩人差"+between.getYears()+"年"+between.getMonths()+"月"+between.getDays()+"天");

    }
    //不同的姓各自有多少学生？
    public static void tongjiXing( ArrayList<Student> list){
        //Map  键 组名   值 :ArrayList<Student>
        TreeMap<Character, ArrayList<Student>> map = new TreeMap<>();
        for (Student student : list) {
            char xing = student.getName().charAt(0);
            //判断map中是否有这个组
            boolean flag = map.containsKey(xing);
            if(flag){
                ArrayList<Student> list1 = map.get(xing);
                //有 将学生添加到集合中
                list1.add(student);
                //用行的集合 替换 老的集合
            }else {  //没有  创建一个新的集合 存入map中
                ArrayList<Student> list1 = new ArrayList<>(List.of(student));
                map.put(xing,list1);
            }
        }
        map.forEach((k,v)-> System.out.println(k+"组的人有"+v));
        //Map 键 组名 值 平均年龄
        Map<Character, Integer> ageMap = new TreeMap<>();
        map.forEach((k,v)-> {
            ArrayList<Student> list1 = v;
            int count=0;
            for (Student student : list1) {
                count++;
            }
            ageMap.put(k,count);
        });
        ageMap.forEach((k,v)-> System.out.println("姓:"+k+"的人有"+v+"个"));



    }
    //不同的年龄段有多少人？
    public static void tongjiNianlingDuan( ArrayList<Student> list){
        //Map  键 组名   值 :ArrayList<Student>
        TreeMap<Integer, ArrayList<Student>> map = new TreeMap<>();
        for (Student student : list) {
            int age = student.getAge();
            //判断map中是否有这个组
            boolean flag = map.containsKey(age);
            if(flag){
                ArrayList<Student> list1 = map.get(age);
                //有 将学生添加到集合中
                list1.add(student);
                //用行的集合 替换 老的集合
            }else {  //没有  创建一个新的集合 存入map中
                ArrayList<Student> list1 = new ArrayList<>(List.of(student));
                map.put(age,list1);
            }
        }
        map.forEach((k,v)-> System.out.println(k+"组的人有"+v));
        //Map 键 组名 值 平均年龄
        Map<Integer, Integer> ageMap = new TreeMap<>();
        map.forEach((k,v)-> {
            ArrayList<Student> list1 = v;
            int count=0;
            for (Student student : list1) {
                count++;
            }
            ageMap.put(k,count);
        });
        ageMap.forEach((k,v)-> System.out.println(+k+"岁的人有"+v+"个"));



    }
}
